package com.szw.dlyk.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.szw.dlyk.constant.Constant;
import com.szw.dlyk.domain.ActivityRemark;
import com.szw.dlyk.query.ActivityRemarkQuery;
import com.szw.dlyk.service.ActivityRemarkService;
import com.szw.dlyk.mapper.ActivityRemarkMapper;
import com.szw.dlyk.util.JwtUtil;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
* @author asus
* @description 针对表【t_activity_remark(市场活动备注表)】的数据库操作Service实现
* @createDate 2024-01-21 18:16:50
*/
@Service
public class ActivityRemarkServiceImpl extends ServiceImpl<ActivityRemarkMapper, ActivityRemark>
    implements ActivityRemarkService{

    @Resource
    private ActivityRemarkMapper activityRemarkMapper;
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int saveActivityRemark(ActivityRemarkQuery activityRemarkQuery) {
        ActivityRemark activityRemark = new ActivityRemark();

        //把ActivityRemarkQuery对象里面的属性数据复制到TActivityRemark对象里面去(复制要求：两个对象的属性名相同，属性类型要相同，这样才能复制)
        BeanUtils.copyProperties(activityRemarkQuery, activityRemark);

        activityRemark.setCreateTime(new Date()); //创建时间

        //登录人的id
        Integer loginUserId = JwtUtil.parseUserFromJWT(activityRemarkQuery.getToken()).getId();
        activityRemark.setCreateBy(loginUserId); //创建人

        return activityRemarkMapper.insertSelective(activityRemark);
    }

    @Override
    public PageInfo<ActivityRemark> getActivityRemarkByPage(Integer current, ActivityRemarkQuery activityRemarkQuery) {
        // 1.设置PageHelper
        PageHelper.startPage(current, Constant.PAGE_SIZE);
        // 2.查询
        List<ActivityRemark> list = activityRemarkMapper.selectActivityRemarkByPage(activityRemarkQuery);
        // 3.封装分页数据到PageInfo
        PageInfo<ActivityRemark> info = new PageInfo<>(list);
        return info;
    }

    @Override
    public ActivityRemark getActivityRemarkById(Integer id) {
        return activityRemarkMapper.selectByPrimaryKey(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateActivityRemark(ActivityRemarkQuery activityRemarkQuery) {
        ActivityRemark activityRemark = new ActivityRemark();

        //把ActivityRemarkQuery对象里面的属性数据复制到TActivityRemark对象里面去(复制要求：两个对象的属性名相同，属性类型要相同，这样才能复制)
        BeanUtils.copyProperties(activityRemarkQuery, activityRemark);

        activityRemark.setEditTime(new Date()); //编辑时间

        //登录人的id
        Integer loginUserId = JwtUtil.parseUserFromJWT(activityRemarkQuery.getToken()).getId();
        activityRemark.setEditBy(loginUserId); //创建人

        return activityRemarkMapper.updateByPrimaryKeySelective(activityRemark);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int delActivityRemarkById(Integer id) {
        //逻辑删除：不删数据，只是修改一下数据的状态，数据依然还在表里面；
        //物理删除：真正的把数据从表里面删掉
        ActivityRemark activityRemark = new ActivityRemark();
        activityRemark.setId(id);
        activityRemark.setDeleted(1); //删除状态（null或者0正常，1删除）
        return activityRemarkMapper.updateByPrimaryKeySelective(activityRemark);
    }
}




